#[
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.
]#

import
  xxtea,
  os,
  terminal,
  strenc

proc red(text: string): string = "\e[30m\e[101m" & text & "\e[0m " # makes background red
proc green(text: string): string = "\e[30m\e[42m" & text & "\e[0m " # makes background green

try:
  let arg1 = paramStr(1)
  if arg1 == "--help":
    echo("* Usage:")
    echo("    priman [flag...] <file>\n")
    echo("* Priman is a tool for file encrypting using XXTEA algorithm.\n")
    echo("* Flags:")
    echo("    -e            Input file to encrypt.")
    echo("    -d            Input file to decrypt.")
    echo("    --help          Show help.")
    echo("    --version       Show the version.\n")


  elif arg1 == "--version":
    echo("__________         __")
    echo("\\______   \\_______|__| _____ _____    ____")
    echo(" |     ___/\\_  __ \\  |/     \\__   \\  /    \\ ")
    echo(" |    |     |  | \\/  |  Y Y  \\/ __ \\|   |  \\")
    echo(" |____|     |__|  |__|__|_|__(_____/|___|__/")
    echo("                                               ")
    echo("Priman v1.0.0\n\nThis software may be freely redistributed under\nthe terms of the GNU General Public License version 3.0 or later.")

  elif arg1 == "-e":
    let
      inputFile = paramStr(2)
      fileContent = readFile(inputFile)
      key = terminal.readPasswordFromStdin("Set the password: ")
      keyRepeat = terminal.readPasswordFromStdin("Repeat the password: ")
    if key != keyRepeat:
      echo("Err:".red & "Keys didn't match!")
      system.quit()
    writeFile(inputFile, xxtea.encrypt(fileContent, key))
    echo("Ok:".green & "File successfully encrypted!")
  
  elif arg1 == "-d":
    let
      inputFile = paramStr(2)
      fileContent = readFile(inputFile)
      key = terminal.readPasswordFromStdin("Enter the password: ")
      decrypted = xxtea.decrypt(fileContent, key)
    if decrypted == "":
      echo("Err:".red & "Decryption failed. Wrong password.")
    else:
      writeFile(inputFile, decrypted)
      echo("Ok:".green & "File successfully decrypted!")
  
  else:
    echo("Err:".red & "Flag \"", arg1, "\" doesn't exist. Type \"--help\" for help.")
except IndexDefect:
  if paramCount() < 1:
    echo("Err:".red & "No flag is given. Type \"--help\" for usage.")
    system.quit()
  echo("Err:".red & "Argument must be given.\nUsage: " & paramStr(1) & " <file>")
except ValueError:
  echo("Err:".red & "Decryption failed.\nWrong password.")
except IOError:
  echo("Err:".red & "Failed to read \"", paramStr(2), "\". Does it exist? Do you have permissions for that file?")
